package com.wcards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcards.config.R;
import com.wcards.entity.AdoptApplicationInfo;
import com.wcards.entity.AdoptRecordInfo;
import com.wcards.mapper.AdoptApplicationInfoMapper;
import com.wcards.service.AdoptApplicationInfoService;
import com.wcards.service.AdoptRecordInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
领养申请信息表
 * @author q'y
 */
@Service
public class AdoptApplicationInfoServiceImpl extends ServiceImpl<AdoptApplicationInfoMapper, AdoptApplicationInfo> implements AdoptApplicationInfoService {
    @Autowired
    private AdoptApplicationInfoMapper adoptApplicationInfoMapper;
    @Autowired
    private AdoptRecordInfoService adoptRecordInfoService;
    @Autowired
    private AdoptApplicationInfoService adoptApplicationInfoService;
    /**
    申请领养*/
    @Override
    public R<String> insertOne(AdoptApplicationInfo aaInfo, HttpServletRequest request) {
        //获取当前申请用户的id
        HttpSession session = request.getSession();
        Integer userId = (Integer)session.getAttribute("userId");
        aaInfo.setUserId(userId);
        //设置申请状态默认为处理中
        aaInfo.setApplicationStatus(0);
        adoptApplicationInfoMapper.insert(aaInfo);
        return R.success("申请成功");
    }

    /**
    处理申请状态*/
    @Override
    public R<String> updateOne(AdoptApplicationInfo aaInfo) {
        Integer isRecord = aaInfo.getApplicationStatus();
        //如果isRecord为1 证明为已经通过申请 即为用户新增一个领养记录
        if (isRecord==1)
        {
            AdoptRecordInfo info=new AdoptRecordInfo();
            info.setUserId(aaInfo.getUserId());
            info.setAnimalNumbers(aaInfo.getAnimalNumbers());
            adoptRecordInfoService.save(info);
            adoptApplicationInfoService.updateById(aaInfo);
        }
        return R.success("处理成功");
    }

    /**
    分页查询所有申请
    */
    @Override
    public R<Page<AdoptApplicationInfo>> listAll(Integer current, Integer size) {
        Page<AdoptApplicationInfo> page=new Page<>(current,size);
        adoptApplicationInfoService.page(page,null);
        return R.success(page);
    }

    @Override
    public Page<AdoptApplicationInfo> listByUid(Integer current, Integer size, Integer userId) {
        Page<AdoptApplicationInfo> page=new Page<>(current,size);
        QueryWrapper<AdoptApplicationInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Page<AdoptApplicationInfo> page1 = adoptApplicationInfoService.page(page, wrapper);
        return adoptApplicationInfoService.page(page, wrapper);
    }


}
